import { Component, OnInit, Input } from '@angular/core';

import {Hero} from '../hero';

import {ActivatedRoute} from '@angular/router';
import {Location} from '@angular/common';
import {HeroService} from '../hero.service';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {

  // ActivatedRoute保存着到该组件的路由信息，可以通过它拿到路由信息
  // location 是一个angular服务，与浏览器打交道
  constructor(private route:ActivatedRoute,
  private heroService:HeroService,
  private location:Location
  ) { }

  ngOnInit() {
    this.getHero();
  }

  //@Input装饰器，用于输入绑定，当使用实现绑定的时候会输入这个值
  //使用@Input装饰器，需要import from angular core
  @Input() hero:Hero;

  getHero():void{
    const id =  +this.route.snapshot.paramMap.get("id");
    this.heroService.getHero(id).subscribe(hero => this.hero = hero);
  }

  goBack():void{
    this.location.back();
  }
}
